/**
 * 
 */
package com.mbengue.corp.apr.ra.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import com.mbengue.corp.apr.ra.core.Article;
import com.mbengue.corp.apr.ra.core.ArticleMembre;
import com.mbengue.corp.apr.ra.core.ArticleMembreId;
import com.mbengue.corp.apr.ra.core.Membre;
import com.mbengue.corp.apr.ra.dao.ArticleDao;
import com.mbengue.corp.apr.ra.dao.DAOFactory;
import com.mbengue.corp.apr.ra.dao.impl.ArticleMembreDaoImpl;

/**
 * @author MMB11447
 *
 */
@ManagedBean(name = "pvReunionBean")
@SessionScoped
public class PvReunionBean implements Serializable {
  private static final long serialVersionUID = 1L;
  private Article article;
  private Date debut;
  private Date fin;
  private List<Article> results;
  private ArticleDao articleDao;
  private String titre;
  private String type;

  public PvReunionBean() {
    type = "date";
    article = new Article();
    articleDao = DAOFactory.getArticleDao();
  }

  public String enregistrer() {
    String redirect = "#";
    if (article.getTitre().isEmpty() || article.getResume().isEmpty() || article.getContenu().isEmpty()) {
      FacesContext.getCurrentInstance().addMessage(null,
          new FacesMessage(FacesMessage.SEVERITY_ERROR, "Tous les champs sont obligatoires", ""));
      return redirect;
    }
    //
    try {
      Membre connectedUser = ((LoggingBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
          .get("logging")).getConnectedUser();
      article.setDateDeCreation(new Date());
      article.setType("PV Reunion");
      articleDao.ajouterArticle(article);
      ArticleMembreId id = new ArticleMembreId(connectedUser.getEmail(), article.getTitre());
      ArticleMembre am = new ArticleMembre(id, article, connectedUser);
      new ArticleMembreDaoImpl().ajouter(am);
      FacesContext.getCurrentInstance().addMessage(null,
          new FacesMessage(FacesMessage.SEVERITY_INFO, "PV enregistré", "avec succés!"));
      redirect = "/pages/pvReunion.xhtml?faces-redirect=true";
    } catch (Exception e) {
      FacesContext.getCurrentInstance().addMessage(null,
          new FacesMessage(FacesMessage.SEVERITY_ERROR, "Un erreur est survenu. Echec de l'operation!", ""));
    } finally {
      article = new Article();
    }
    return redirect;
  }

  public void rechercherArticlesParDate() {
    results = new ArrayList<Article>();
    results = articleDao.findArticlesParDates(debut, fin, "PV Reunion");
  }

  public void rechercherArticlesParTitre() {
    results = new ArrayList<Article>();
    results = articleDao.findArticlesParTitre(titre, "PV Reunion");
  }

  public List<Article> getResults() {
    return results;
  }

  public Date getDebut() {
    return debut;
  }

  public void setDebut(Date aDebut) {
    debut = aDebut;
  }

  public Date getFin() {
    return fin;
  }

  public void setFin(Date aFin) {
    fin = aFin;
  }

  public String getTitre() {
    return titre;
  }

  public void setTitre(String aTitre) {
    titre = aTitre;
  }

  public String getType() {
    return type;
  }

  public void setType(String aType) {
    type = aType;
  }

  public List<SelectItem> getTypes() {
    List<SelectItem> types = new ArrayList<SelectItem>();
    types.add(new SelectItem("date", "Par Dates"));
    types.add(new SelectItem("titre", "Par Titre"));
    return types;
  }

  public boolean getDoSearchByDate() {
    return type.equals("date");
  }

  public boolean getDoSearchByTitle() {
    return !getDoSearchByDate();
  }

  /**
   * @return the article
   */
  public Article getArticle() {
    return article;
  }

  /**
   * @param article the article to set
   */
  public void setArticle(Article article) {
    this.article = article;
  }

  /**
   * @return the articles
   */
  public List<Article> getArticles() {
    try {
      return articleDao.tousLesPvDeReunion();
    } catch (Exception e) {
      return null;
    }
  }
}
